package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.File;
import com.zhejiangzhengyuan.municipal_common.entity.SubsidiaryFacilitiesGis;
import com.zhejiangzhengyuan.municipal_common.entity.SubsidiaryFacilitiesGisFileMapping;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.SubsidiaryFacilitiesGisVo;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.SubsidiaryFacilitiesGisFileMappingMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.SubsidiaryFacilitiesGisMapper;
import com.zhejiangzhengyuan.municipal_common.service.SubsidiaryFacilitiesGisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.utils.FileDuplicateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;
import static com.zhejiangzhengyuan.municipal_common.utils.StaticConstants.WAY_MODULE;

/**
 * GIS数据库中附属设施表 服务实现类
 *
 * @author linju
 * @since 2020-01-13
 */
@Service
public class SubsidiaryFacilitiesGisServiceImpl
    extends ServiceImpl<SubsidiaryFacilitiesGisMapper, SubsidiaryFacilitiesGis>
    implements SubsidiaryFacilitiesGisService {

  @Autowired private SubsidiaryFacilitiesGisMapper subsidiaryFacilitiesGisMapper;

  @Autowired
  private SubsidiaryFacilitiesGisFileMappingMapper subsidiaryFacilitiesGisFileMappingMapper;

  @Autowired private FileMapper fileMapper;
  @Autowired private FileDuplicateCode fileDuplicateCode;

  @Override
  public IPage<SubsidiaryFacilitiesGisVo> selectAllSubsidiaryFacilitiesGis(
      Page<SubsidiaryFacilitiesGisVo> subsidiaryFacilitiesGisPage) {
    Page<SubsidiaryFacilitiesGisVo> voPage =
        subsidiaryFacilitiesGisMapper.selectAllSubsidiaryFacilitiesGis(subsidiaryFacilitiesGisPage);
    List<SubsidiaryFacilitiesGisVo> records = voPage.getRecords();
    for (SubsidiaryFacilitiesGisVo subsidiaryFacilitiesGisVo : records) {
      List<String> strings = fileMapper.selectBySid(subsidiaryFacilitiesGisVo.getId());
      if (strings != null && strings.size() != 0) {
        subsidiaryFacilitiesGisVo.setLink(strings);
      }
    }
    return voPage;
  }

  @Override
  public int updateSubsidiaryFacilitiesGis(FileDto dto) {
    MultipartFile multipartFile = dto.getFile();
    Long id = dto.getId();
    if (multipartFile == null) {
      return 0;
    }
    // 如果第一次没有上传文件
    File file = new File();
    if (fileDuplicateCode.insertFileDuplicateCode(file, multipartFile, WAY_MODULE) > 0) {
      SubsidiaryFacilitiesGisFileMapping subsidiaryFacilitiesGisFileMapping =
          new SubsidiaryFacilitiesGisFileMapping();
      subsidiaryFacilitiesGisFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
      subsidiaryFacilitiesGisFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
      subsidiaryFacilitiesGisFileMapping.setSubsidiaryFacilitiesGisId(id);
      subsidiaryFacilitiesGisFileMapping.setFileId(file.getId());
      subsidiaryFacilitiesGisFileMappingMapper.insert(subsidiaryFacilitiesGisFileMapping);
    }
    return 1;
  }
}
